﻿using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FranceTriple.Dal;
using TeamFranceTriple.Config;

namespace TeamFranceTriple
{
    public partial class _Default : BasePage
    {
        // Il est interdit de traduire dans quelle langue que ce soit "Team France Triple", c'est un nom propre.
        private static String prefixTitle = "\"Team France Triple\"" + " - ";
        private String PortalLocalized = "Portal".ToLocalizedString();
        private String ConnexionLocalized = "Connexion".ToLocalizedString();
        private String RegisterLocalized = "Register".ToLocalizedString();
        private String FriendlyTeamLocalized = "FriendlyTeam".ToLocalizedString();
        private String MemberListLocalized = "MemberList".ToLocalizedString();
        private String FanListLocalized = "FanList".ToLocalizedString();
        private String AboutLocalized = "About".ToLocalizedString();
        private String MessageListLocalized = "MessageList".ToLocalizedString();
        private String UserProfileLocalized = "UserProfile".ToLocalizedString();
        private String PwdChangeLocalized = "PwdChange".ToLocalizedString();
        private String QuestionChangeLocalized = "QuestionChange".ToLocalizedString();
        private String PwdChangeSuccessLocalized = "PwdChangeSuccess".ToLocalizedString();
        private String PwdRecoveryLocalized = "PwdRecovery".ToLocalizedString();
        private String AwaitingUsersLocalized = "AwaitingUsers".ToLocalizedString();
        private String ImageLocalized = "Image".ToLocalizedString();
        private String EventLocalized = "Event".ToLocalizedString();
        private String ForumLocalized = "Forum".ToLocalizedString();
        private String SearchLocalized = "Search".ToLocalizedString();

        protected void Page_load(object sender, EventArgs e)
        {
            //Nom de la page de destination
            String page = String.Empty;
            if (Page.RouteData.Values["targetPage"] == null)
                page = TeamFranceTripleConfig.WebConfig.DefaultPage;
            else
                page = Page.RouteData.Values["targetPage"].ToString();

            // Mise à jour du compteur du maximum d'utilisateurs connectés
            ApplicationSettingDal.MaximumOnlineUsers = ForumInfo.TotalCurrentlyConnectedUsers;

            //Chargement des controls de la page demandées.
            var bodyContent = (ContentPlaceHolder)ThisMaster.FindControl("MainContent");
            switch (page)
            {
                case "logout":
                    Session.Abandon();
                    Response.Redirect("~/");
                    break;
                case "home":
                    var home = (Home2012)LoadControl("~/Pages/Home/Home.2012.ascx");
                    bodyContent.Controls.Add(home);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    Page.Title = prefixTitle + PortalLocalized;
                    break;
                case "connexion":
                    var login = (TeamFranceTriple.Connexion)LoadControl("~/Account/Connexion.ascx");
                    login.UserAddressIp = ThisMaster.Requester.IpAddress;
                    bodyContent.Controls.Add(login);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    Page.Title = prefixTitle + ConnexionLocalized;
                    break;
                case "register":
                    var register = (TeamFranceTriple.Register)LoadControl("~/Account/Register.ascx");
                    register.UserAddressIp = ThisMaster.Requester.IpAddress;
                    bodyContent.Controls.Add(register);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    Page.Title = prefixTitle + RegisterLocalized;
                    break;
                case "fanlist":
                    var fanlist = (TeamFranceTriple.MemberList)LoadControl("~/Account/MemberList.ascx");
                    fanlist.TargetRole = "Fans";
                    fanlist.GroupingText = FanListLocalized;
                    bodyContent.Controls.Add(fanlist);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    Page.Title = prefixTitle + FanListLocalized;
                    break;
                case "memberlist":
                    var memberList = (TeamFranceTriple.MemberList)LoadControl("~/Account/MemberList.ascx");
                    memberList.TargetRole = "Member";
                    memberList.GroupingText = MemberListLocalized;
                    bodyContent.Controls.Add(memberList);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    Page.Title = prefixTitle + MemberListLocalized;
                    break;
                case "about":
                    var about = (About)LoadControl("~/Pages/About/About.ascx");
                    bodyContent.Controls.Add(about);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    Page.Title = prefixTitle + AboutLocalized;
                    break;
                case "shoutboxlist":
                    var shoutboxlist = (ShoutboxList)LoadControl("~/Pages/Master/ShoutboxList.ascx");
                    bodyContent.Controls.Add(shoutboxlist);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    Page.Title = prefixTitle + MessageListLocalized;
                    break;
                case "userprofile":
                    if (ThisMaster.Requester.IsAuthenticated)
                    {
                        var userprofile = (TeamFranceTriple.Account.UserProfileControl)LoadControl("~/Account/UserProfileControl.ascx");
                        userprofile.UserAddressIp = ThisMaster.Requester.IpAddress;
                        bodyContent.Controls.Add(userprofile);
                        this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                        Page.Title = prefixTitle + UserProfileLocalized;
                        break;
                    }
                    else
                        Response.Redirect(String.Format("~/{0}/connexion/?returnurl=~/{0}/userprofile", ThisMaster.Requester.Culture));
                    break;
                case "changepassword":
                    if (ThisMaster.Requester.IsAuthenticated)
                    {
                        var changepassword = (TeamFranceTriple.Account.ChangePassword)LoadControl("~/Account/ChangePassword.ascx");
                        bodyContent.Controls.Add(changepassword);
                        //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                        Page.Title = prefixTitle + PwdChangeLocalized;
                        break;
                    }
                    else
                    Response.Redirect(String.Format("~/{0}/connexion/?returnurl=~/{0}/changepassword", ThisMaster.Requester.Culture));
                    break;
                case "changepasswordquestionanswer":
                    if (ThisMaster.Requester.IsAuthenticated)
                    {
                        var changepasswordquestionanswer = (TeamFranceTriple.Account.ChangePasswordQuestionAnswer)LoadControl("~/Account/changepasswordquestionanswer.ascx");
                        bodyContent.Controls.Add(changepasswordquestionanswer);
                        //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                        Page.Title = prefixTitle + QuestionChangeLocalized;
                        break;
                    }
                    else
                    Response.Redirect(String.Format("~/{0}/connexion/?returnurl=~/{0}/changepasswordquestionanswer", ThisMaster.Requester.Culture));

                    break;
                case "changepasswordsuccess":
                    if (ThisMaster.Requester.IsAuthenticated)
                    {
                        var changepasswordsuccess = (TeamFranceTriple.Account.ChangePasswordSuccess)LoadControl("~/Account/ChangePasswordSuccess.ascx");
                        bodyContent.Controls.Add(changepasswordsuccess);
                        //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                        Page.Title = prefixTitle + PwdChangeSuccessLocalized;
                        break;
                    }
                    else
                    Response.Redirect(String.Format("~/{0}/connexion/?returnurl=~/{0}/changepasswordsuccess", ThisMaster.Requester.Culture));
                    break;
                case "pwdrecovery":
                    var pwdrecovery = (PasswordRecovery)LoadControl("~/Account/PasswordRecovery.ascx");
                    bodyContent.Controls.Add(pwdrecovery);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    Page.Title = prefixTitle + PwdRecoveryLocalized;
                    break;
                case "awaitingusers":
                    if (ThisMaster.Requester.Roles.Contains("RoleAdminForum"))
                    {
                        var awaitingusers = (AwaitingUsers)LoadControl("~/Account/AwaitingUsers.ascx");
                        bodyContent.Controls.Add(awaitingusers);
                        //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                        Page.Title = prefixTitle + AwaitingUsersLocalized;
                    }
                    else
                    Response.Redirect(String.Format("~/{0}/connexion/?returnurl=~/{0}/awaitingusers", ThisMaster.Requester.Culture));
                    break;
                case "picturelist":
                    if (ThisMaster.Requester.Roles.Contains("Member"))
                    {
                        var picturelist = (PictureList)LoadControl("~/Pages/PictureGallery/PictureList.ascx");
                        bodyContent.Controls.Add(picturelist);
                        //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                        Page.Title = prefixTitle + ImageLocalized;
                    }
                    else
                    Response.Redirect(String.Format("~/{0}/connexion/?returnurl=~/{0}/picturelist", ThisMaster.Requester.Culture));
                    break;
                case "eventlist":
                    if (ThisMaster.Requester.Roles.Contains("Member"))
                    {
                        var eventlist = (EventList)LoadControl("~/Pages/Events/EventList.ascx");
                        bodyContent.Controls.Add(eventlist);
                        //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                        Page.Title = prefixTitle + EventLocalized;
                    }
                    else
                    Response.Redirect(String.Format("~/{0}/connexion/?returnurl=~/{0}/eventlist", ThisMaster.Requester.Culture));
                    break;
                case "forum":
                    var forum = (ForumList)LoadControl("~/Pages/Forum/ForumList.ascx");
                    bodyContent.Controls.Add(forum);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    Page.Title = prefixTitle + ForumLocalized;
                    break;
                case "topic":
                    var topic = (ForumTopicList)LoadControl("~/Pages/Forum/ForumTopicList.ascx");
                    bodyContent.Controls.Add(topic);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    break;
                case "search":
                    var search = (ucForumSearch)LoadControl("~/Pages/Forum/ucForumSearch.ascx");
                    bodyContent.Controls.Add(search);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    break;
                case "topicdetail":
                    if (ThisMaster.Requester.IsAuthenticated)
                    {
                        var topicdetail = (TeamFranceTriple.ForumTopicDetail)LoadControl("~/Pages/Forum/ForumTopicDetail.ascx");
                        bodyContent.Controls.Add(topicdetail);
                        break;
                    }
                    else
                    Response.Redirect(String.Format("~/{0}/connexion/?returnurl=~/{1}", ThisMaster.Requester.Culture, Request.RawUrl.ToString()));
                    break;
                case "messagedetail":
                    if (ThisMaster.Requester.IsAuthenticated)
                    {
                        var messagedetail = (TeamFranceTriple.MessageDetail)LoadControl("~/Base/ascx/MessageDetail.ascx");
                        messagedetail.UserAddressIp = ThisMaster.Requester.IpAddress;
                        bodyContent.Controls.Add(messagedetail);
                        break;
                    }
                    else
                    Response.Redirect(String.Format("~/{0}/connexion/?returnurl=~/{1}", ThisMaster.Requester.Culture, Request.RawUrl.ToString()));
                    break;
                case "forumpost":
                    var post = (ForumPostList)LoadControl("~/Pages/Forum/ForumPostList.ascx");
                    post.UserAddressIp = ThisMaster.Requester.IpAddress;
                    bodyContent.Controls.Add(post);
                    //this.Form.DefaultButton = userprofile.FindControl("SaveButton").UniqueID;
                    break;
                default:
                    // Page inconnue ? Erreur 404 pour assurer la mise à jour des moteurs de recherche
                    throw new HttpException(404, "This page don't exists anymore. (_default_switch(page)): " + page.TextToHtml());
            }
        }

    }
}
